package com.atguigu.day03;

import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink10_TransForm_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.TODO 使用process实现flatMap的功能 将数据转为Tuple2元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneStream = streamSource.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        //4.将相同单词的数据聚合到一块
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordToOneStream.keyBy(0);

        //5.TODO 使用Process实现Sum的功能
        keyedStream.process(new KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Integer>>() {
            //定义一个累加器，保存上一次累加后结果 bug 这个遍量中保存的至并没有区分key，所有的key都可以获取到这个值并改变
//            private Integer lastSum = 0;
            private HashMap<String, Integer> lastSumMap = new HashMap<>();

            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                //1.判断当前单词是否在Map中并作为key
                if (lastSumMap.containsKey(value.f0)){
                    //2.根据key取出之前累加后的结果
                    Integer lastSum = lastSumMap.get(value.f0);
                    //3.在此基础上加1
                    Integer curSum = lastSum + 1;
                    //4.将累加后的值输出并更新到Map中
                    out.collect(Tuple2.of(value.f0,curSum));
                    lastSumMap.put(value.f0, curSum);
                }else {
                    //如果当前这条数据没有在Map中保存，证明是这个key的第一条数据
                    lastSumMap.put(value.f0, 1);
                    out.collect(Tuple2.of(value.f0, 1));
                }
            }
        }).print();

        env.execute();
    }
}
